﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace WebPresentation
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ImageButtonadd_Click(object sender, ImageClickEventArgs e)
        {
            UserBO userBO = new UserBO();

            int rID = Convert.ToInt32(ddlRole.SelectedValue);
            string firstName = txtFirstName.Text;
            string lastName = txtLastName.Text;
            string address = txtAddress.Text;
            string phone = txtPhone.Text;
            int depID = Convert.ToInt32(ddldepartment.SelectedValue);
            int pID = Convert.ToInt32(ddlposition.SelectedValue);
            string email = txtmail.Text;
            string password = txtpass.Text;

            if (userBO.AddNewUser(rID, firstName, lastName, address, phone, depID, password, email, pID) > 0)
            {
                Label10.Text = "Add successfully!";
            }
            else
            {
                Label10.Text = "Add failed";
            }
            dtguser.DataBind();
        }

        protected void ImageButtonedit_Click(object sender, ImageClickEventArgs e)
        {
            if (dtguser.SelectedRow != null)
            {
                int uID = Int32.Parse(dtguser.SelectedRow.Cells[1].Text);
                int rID = Convert.ToInt32(ddlRoleEdit.SelectedValue);
                string firstName = txtFirstNameEdit.Text;
                string lastName = txtLastNameEdit.Text;
                string address = txtAddressEdit.Text;
                string phone = txtPhoneEdit.Text;
                int depID = Convert.ToInt32(ddlrepEdit.SelectedValue);
                int pID = Convert.ToInt32(ddlposEdit.SelectedValue);
                string email = txtMailEdit.Text;
                string password = txtPassEdit.Text;


                if (new UserBO().Update(rID, firstName, lastName, password, email, pID, depID, phone, address, uID) > 0)
                {
                    dtguser.DataBind();
                    Label10.Text = "Update successfully.";
                }
                else
                {
                    Label10.Text = "Update failed!";
                }

            }
        }

        protected void LinkButtondel_Click(object sender, EventArgs e)
        {
            if (dtguser.SelectedRow != null)
            {
                UserBO userBO = new UserBO();
                try
                {
                    if (userBO.Delete(Int32.Parse(dtguser.SelectedRow.Cells[1].Text)) > 0)
                    {
                        Label10.Text = "Delete successfully.";
                        dtguser.DataBind();
                    }
                    else
                    {
                        Label10.Text = "Delete failed!";
                    }
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script> alert('Warning: Please delete all the data that refer to this record first!')</script>");
                }
            }
            else
            {

            }
        }

        protected void LinkButtonedit_Click(object sender, EventArgs e)
        {
            if (dtguser.SelectedRow != null) {
                DataTable dt = new UserBO().GetByID(Convert.ToInt32(dtguser.SelectedRow.Cells[1].Text));

                txtFirstNameEdit.Text = dt.Rows[0]["uFName"].ToString();
                txtLastNameEdit.Text = dt.Rows[0]["uLName"].ToString();
                txtAddressEdit.Text = dt.Rows[0]["uAddress"].ToString();;
                txtPhoneEdit.Text = dt.Rows[0]["uPhone"].ToString(); ;
                txtMailEdit.Text = dt.Rows[0]["uEmail"].ToString();
                txtPassEdit.Text = dt.Rows[0]["uPassword"].ToString();
            }
        }

        protected void dtguser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dtguser.SelectedRow != null) {
                DataTable dt = new UserBO().GetByID(Convert.ToInt32(dtguser.SelectedRow.Cells[1].Text));

                txtFirstNameEdit.Text = dt.Rows[0]["uFName"].ToString();
                txtLastNameEdit.Text = dt.Rows[0]["uLName"].ToString();
                txtAddressEdit.Text = dt.Rows[0]["uAddress"].ToString();;
                txtPhoneEdit.Text = dt.Rows[0]["uPhone"].ToString(); ;
                txtMailEdit.Text = dt.Rows[0]["uEmail"].ToString();
                txtPassEdit.Text = dt.Rows[0]["uPassword"].ToString();
            }
        
        }


    }
}